/*
 * To change this template, choose Tools | Templates
 * and open the template in the editor.
 */
package servlet;

import com.oreilly.servlet.MultipartRequest;
import java.io.File;
import java.io.IOException;
import javax.servlet.ServletException;
import javax.servlet.http.HttpServletRequest;
import javax.servlet.http.HttpServletResponse;
import javax.servlet.http.HttpSession;
import viviendas.Conexion;

/**
 *
 * @author x
 */
public class Imagenes extends Conexion {

    /**
     * Processes requests for both HTTP
     * <code>GET</code> and
     * <code>POST</code> methods.
     *
     * @param request servlet request
     * @param response servlet response
     * @throws ServletException if a servlet-specific error occurs
     * @throws IOException if an I/O error occurs
     */
    protected void processRequest(HttpServletRequest request, HttpServletResponse response)
            throws ServletException, IOException {
        HttpSession session = request.getSession();
        int id_vivienda = 0;
        try {
            new File(getDireccion("/img/viviendas")).mkdir();
            MultipartRequest multi = new MultipartRequest(request, getDireccion("/img/viviendas"), 5 * 1024 * 1024);

            if (!multi.getParameter("id_vivienda").equals("null")) {
                
                id_vivienda = Integer.parseInt(multi.getParameter("id_vivienda"));
                String foto1 = multi.getFile("foto1") != null ? multi.getFile("foto1").getName() : "";
                String foto2 = multi.getFile("foto2") != null ? multi.getFile("foto2").getName() : "";
                String foto3 = multi.getFile("foto3") != null ? multi.getFile("foto3").getName() : "";
                String foto4 = multi.getFile("foto4") != null ? multi.getFile("foto4").getName() : "";
                String foto5 = multi.getFile("foto5") != null ? multi.getFile("foto5").getName() : "";

                if (!foto1.equals("")) {
                    save("insert into imagenes(id_vivienda, url_img) values(" + id_vivienda + ", '" + foto1 + "')");
                }
                if (!foto2.equals("")) {
                    save("insert into imagenes(id_vivienda, url_img) values(" + id_vivienda + ", '" + foto2 + "')");
                }
                if (!foto3.equals("")) {
                    save("insert into imagenes(id_vivienda, url_img) values(" + id_vivienda + ", '" + foto3 + "')");
                }
                if (!foto4.equals("")) {
                    save("insert into imagenes(id_vivienda, url_img) values(" + id_vivienda + ", '" + foto4 + "')");
                }
                if (!foto5.equals("")) {
                    save("insert into imagenes(id_vivienda, url_img) values(" + id_vivienda + ", '" + foto5 + "')");
                }
                setSuccessMessage("Imagenes insertadas exitosamente.");
            }
        } catch (Exception ex) {
            System.out.println("Error en mantenimiento imagenes " + ex.getMessage());
            setErrorMessage("Error al guardar las imagenes");
        } finally {
            session.setAttribute("successMessage", getSuccessMessage());
            session.setAttribute("errorMessage", getErrorMessage());
            setSuccessMessage("");
            setErrorMessage("");
            response.sendRedirect("imagenes.jsp?id_vivienda=" + id_vivienda);
        }
    }

    //<editor-fold defaultstate="collapsed" desc="HttpServlet methods. Click on the + sign on the left to edit the code.">
    /**
     * Handles the HTTP
     * <code>GET</code> method.
     *
     * @param request servlet request
     * @param response servlet response
     * @throws ServletException if a servlet-specific error occurs
     * @throws IOException if an I/O error occurs
     */
    @Override
    protected void doGet(HttpServletRequest request, HttpServletResponse response)
            throws ServletException, IOException {
        processRequest(request, response);
    }

    /**
     * Handles the HTTP
     * <code>POST</code> method.
     *
     * @param request servlet request
     * @param response servlet response
     * @throws ServletException if a servlet-specific error occurs
     * @throws IOException if an I/O error occurs
     */
    @Override
    protected void doPost(HttpServletRequest request, HttpServletResponse response)
            throws ServletException, IOException {
        processRequest(request, response);
    }

    /**
     * Returns a short description of the servlet.
     *
     * @return a String containing servlet description
     */
    @Override
    public String getServletInfo() {
        return "Short description";
    }// </editor-fold>
}
